package com.company.Reflection;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.Data;

public class Demo {

    private static final Integer COUNT = 1000000;

    public static void main(String[] args) {
        UserInfo model = new UserInfo();
        model.setAddress(RandomUtil.randomString(10));
        model.setCode(RandomUtil.randomString(10));
        model.setName(RandomUtil.randomString(10));
        model.setIdcard(RandomUtil.randomString(10));
        model.setAge(RandomUtil.randomInt());

        Long start = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
            copy(model);
            // reflect();
        }

        System.out.println("耗时：" + (System.currentTimeMillis() - start));
    }

    private static void copy(UserInfo model) {
        Info data = BeanUtil.copyProperties(model, Info.class);
        data.setAddress(RandomUtil.randomString(10));
        data.setCode(RandomUtil.randomString(10));
        data.setName(RandomUtil.randomString(10));
        data.setIdcard(RandomUtil.randomString(10));
        model = BeanUtil.copyProperties(data, UserInfo.class);
    }

    private static void reflect() {
        Info model = new Info();
        Field[] fields = model.getClass().getDeclaredFields();
        Arrays.stream(fields).forEach(item -> {
            item.setAccessible(true);
            try {
                item.set(model, RandomUtil.randomString(10));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
    }

    @Data
    static class UserInfo implements Serializable {

        private Integer age;

        private String name;

        private String code;

        private String address;

        private String idcard;
    }

    @Data
    static class Info implements Serializable {

        private String name;

        private String code;

        private String address;

        private String idcard;
    }
}
